#region Using References

using System;

#endregion

namespace Beaker
{
	internal static class InternalExtensionMethods
	{
		/// <summary>
		/// Gets the value that is referenced by the weak reference, of when not available, creates it (again).
		/// </summary>
		/// <typeparam name="T"></typeparam>
		/// <param name="reference">The reference.</param>
		/// <param name="create">The action to create the value.</param>
		/// <returns>The requested value.</returns>
		internal static T GetOrCreate<T>( this WeakReference<T> reference, Func<T> create )
			where T : class
		{
			T value;
			if( !reference.TryGetTarget( out value ) )
			{
				value = create();
				reference.SetTarget( value );
			}

			return value;
		}
	}
}